package com.dylan.datacollection;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.dylan.datacollection.db.SharedPreferencesUtils;
import com.dylan.datacollection.net.HttpConfig;
import com.dylan.datacollection.net.HttpHelper;
import com.dylan.datacollection.net.ResponseCallback;
import com.dylan.datacollection.utils.ToastUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    EditText accountEdit;
    EditText pwdEdit;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        context = this;
        accountEdit = (EditText) findViewById(R.id.login_account_editText);
        pwdEdit = (EditText) findViewById(R.id.login_pwd_editText);
//        if (!TextUtils.isEmpty(SharedPreferencesUtils.readToken(context))){
//            Intent intent = new Intent(context,MainActivity.class);
//            startActivity(intent);
//            finish();
//        }
        findViewById(R.id.login_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String pwd = pwdEdit.getText().toString();
                if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(pwd)) {
                    login(account,pwd);
                } else {
                    ToastUtils.showShortMsg(context,getResources().getString(R.string.account_or_password_not_null));
                }
            }
        });
    }
    public void login(String account,String pwd){
        HttpHelper.getInstance().login(account, pwd,new ResponseCallback(){
            @Override
            public void onResponse(Object response, int id) {
                super.onResponse(response, id);
                try {
                    Log.i("LOGIN","response:"+response.toString());
                    JSONObject jsonObject = new JSONObject(response.toString());
                    if (jsonObject.optInt("code") == HttpConfig.SUCCESS){
                        SharedPreferencesUtils.saveToken(context,jsonObject.getJSONObject("data").getString("token"));
                        SharedPreferencesUtils.saveString(context,"user",jsonObject.getJSONObject("data").getString("user"));
                        setResult(RESULT_OK);
//                        Intent intent = new Intent(context,MainActivity.class);
//                        startActivity(intent);
                        finish();
                    }else{
                        ToastUtils.showShortMsg(context,jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
